package com.alibaba.datax.plugin.writer.oraclewriter;
import com.alibaba.datax.common.plugin.RecordReceiver;
import com.alibaba.datax.common.spi.Writer;
import com.alibaba.datax.common.util.Configuration;
import com.alibaba.datax.plugin.rdbms.util.DataBaseType;
import com.alibaba.datax.plugin.rdbms.writer.Key;

import java.util.List;

public class OracleWriter extends Writer {
	private static final DataBaseType DATABASE_TYPE = DataBaseType.Oracle;

	public static class Job extends Writer.Job {
		private Configuration originalConfig = null;
		private OracleCommonWriter.Job commonRdbmsWriterJob;
		private OracleCommonWriter.Job oracleCommonWriterJob;
		String writeMode = null;
        public void preCheck() {
            this.init();
			writeMode=this.originalConfig.getString(Key.WRITE_MODE);
			if(DATABASE_TYPE.equals(DataBaseType.Oracle)&&writeMode!=null&&writeMode.toLowerCase().startsWith("update")){
            	this.oracleCommonWriterJob.writerPreCheck(this.originalConfig, DATABASE_TYPE);
			}else{
				this.commonRdbmsWriterJob.writerPreCheck(this.originalConfig, DATABASE_TYPE);
			}

        }

        @Override
		public void init() {
			this.originalConfig = super.getPluginJobConf();

//			// warn：not like mysql, oracle only support insert mode, don't use
//			String writeMode = this.originalConfig.getString(Key.WRITE_MODE);
//			if (null != writeMode) {
//				throw DataXException
//						.asDataXException(
//								DBUtilErrorCode.CONF_ERROR,
//								String.format(
//										"写入模式(writeMode)配置错误. 因为Oracle不支持配置项 writeMode: %s, Oracle只能使用insert sql 插入数据. 请检查您的配置并作出修改",
//										writeMode));
//			}

			if(DATABASE_TYPE.equals(DataBaseType.Oracle)&&writeMode!=null&&writeMode.toLowerCase().startsWith("update")){
				this.oracleCommonWriterJob = new OracleCommonWriter.Job(
						DATABASE_TYPE);
				this.oracleCommonWriterJob.init(this.originalConfig);
			}else{
				this.commonRdbmsWriterJob = new OracleCommonWriter.Job(
						DATABASE_TYPE);
				this.commonRdbmsWriterJob.init(this.originalConfig);
			}

		}

		@Override
		public void prepare() {
            //oracle实跑先不做权限检查
            //this.commonRdbmsWriterJob.privilegeValid(this.originalConfig, DATABASE_TYPE);
			if(DATABASE_TYPE.equals(DataBaseType.Oracle)&&writeMode!=null&&writeMode.toLowerCase().startsWith("update")){
				this.oracleCommonWriterJob.prepare(this.originalConfig);
			}else{
				this.commonRdbmsWriterJob.prepare(this.originalConfig);
			}

		}

		@Override
		public List<Configuration> split(int mandatoryNumber) {
			if(DATABASE_TYPE.equals(DataBaseType.Oracle)&&writeMode!=null&&writeMode.toLowerCase().startsWith("update")){
				return this.oracleCommonWriterJob.split(this.originalConfig,
						mandatoryNumber);
			}else{
				return this.commonRdbmsWriterJob.split(this.originalConfig,
						mandatoryNumber);
			}

		}

		@Override
		public void post() {
			if(DATABASE_TYPE.equals(DataBaseType.Oracle)&&writeMode!=null&&writeMode.toLowerCase().startsWith("update")){
				this.oracleCommonWriterJob.post(this.originalConfig);
			}else{
				this.commonRdbmsWriterJob.post(this.originalConfig);
			}

		}

		@Override
		public void destroy() {
			if(DATABASE_TYPE.equals(DataBaseType.Oracle)&&writeMode!=null&&writeMode.toLowerCase().startsWith("update")){
				this.oracleCommonWriterJob.destroy(this.originalConfig);
			}else{
				this.commonRdbmsWriterJob.destroy(this.originalConfig);
			}

		}

	}

	public static class Task extends Writer.Task {
		private Configuration writerSliceConfig;
		private OracleCommonWriter.Task commonRdbmsWriterTask;
		private OracleCommonWriter.Task oracleCommonWriterTask;
		String writeMode = null;
		@Override
		public void init() {
			this.writerSliceConfig = super.getPluginJobConf();
			writeMode=this.writerSliceConfig.getString(Key.WRITE_MODE);
			if(DATABASE_TYPE.equals(DataBaseType.Oracle)&&writeMode!=null&&writeMode.toLowerCase().startsWith("update")){
				this.oracleCommonWriterTask = new OracleCommonWriter.Task(DATABASE_TYPE);
				this.oracleCommonWriterTask.init(this.writerSliceConfig);
			}else{
				this.commonRdbmsWriterTask = new OracleCommonWriter.Task(DATABASE_TYPE);
				this.commonRdbmsWriterTask.init(this.writerSliceConfig);
			}


		}

		@Override
		public void prepare() {
			if(DATABASE_TYPE.equals(DataBaseType.Oracle)&&writeMode!=null&&writeMode.toLowerCase().startsWith("update")){
				this.oracleCommonWriterTask.prepare(this.writerSliceConfig);
			}else{
				this.commonRdbmsWriterTask.prepare(this.writerSliceConfig);
			}

		}

		public void startWrite(RecordReceiver recordReceiver) {
			if(DATABASE_TYPE.equals(DataBaseType.Oracle)&&writeMode!=null&&writeMode.toLowerCase().startsWith("update")){
				this.oracleCommonWriterTask.startWrite(recordReceiver,this.writerSliceConfig, super.getTaskPluginCollector());
			}else{
				this.commonRdbmsWriterTask.startWrite(recordReceiver,this.writerSliceConfig, super.getTaskPluginCollector());
			}

		}

		@Override
		public void post() {
			if(DATABASE_TYPE.equals(DataBaseType.Oracle)&&writeMode!=null&&writeMode.toLowerCase().startsWith("update")){
				this.oracleCommonWriterTask.post(this.writerSliceConfig);
			}else{
				this.commonRdbmsWriterTask.post(this.writerSliceConfig);
			}

		}

		@Override
		public void destroy() {
			if(DATABASE_TYPE.equals(DataBaseType.Oracle)&&writeMode!=null&&writeMode.toLowerCase().startsWith("update")){
				this.oracleCommonWriterTask.destroy(this.writerSliceConfig);
			}else{
				this.commonRdbmsWriterTask.post(this.writerSliceConfig);
			}
		}

	}

}
